package com.example.myapplication.data.database

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.Index
import androidx.room.PrimaryKey
import com.example.myapplication.data.model.StorageItem
import java.util.UUID

/**
 * 储物物品数据库实体
 */
@Entity(
    tableName = "storage_items",
    foreignKeys = [
        ForeignKey(
            entity = StorageSpaceEntity::class,
            parentColumns = ["id"],
            childColumns = ["space_id"],
            onDelete = ForeignKey.CASCADE
        )
    ],
    indices = [Index(value = ["space_id"])]
)
data class StorageItemEntity(
    @PrimaryKey 
    val id: String = UUID.randomUUID().toString(),
    
    @ColumnInfo(name = "name")
    val name: String,
    
    @ColumnInfo(name = "description")
    val description: String? = null,
    
    @ColumnInfo(name = "quantity")
    val quantity: Int = 1,
    
    @ColumnInfo(name = "space_id")
    val spaceId: String,
    
    @ColumnInfo(name = "image_path")
    val imagePath: String? = null,
    
    @ColumnInfo(name = "tags")
    val tags: String? = null,
    
    @ColumnInfo(name = "created_at")
    val createdAt: Long = System.currentTimeMillis(),
    
    @ColumnInfo(name = "updated_at")
    val updatedAt: Long = System.currentTimeMillis()
)

/**
 * Entity转换为Model
 */
fun StorageItemEntity.toModel(): StorageItem = StorageItem(
    id = id,
    name = name,
    description = description,
    quantity = quantity,
    spaceId = spaceId,
    imagePath = imagePath,
    tags = tags,
    createdAt = createdAt,
    updatedAt = updatedAt
)

/**
 * Model转换为Entity
 */
fun StorageItem.toEntity(): StorageItemEntity = StorageItemEntity(
    id = id,
    name = name,
    description = description,
    quantity = quantity,
    spaceId = spaceId,
    imagePath = imagePath,
    tags = tags,
    createdAt = createdAt,
    updatedAt = updatedAt
) 